package cn.webrx;
public class DataBase {
	private Customer[] cus;
	private Admin[] admins;
	public  DataBase() {
		this.cus=new Customer[10];
		this.admins =new Admin[3];
		cus[0]=new Customer("jack","123456",1562,"111");
		cus[1]=new Customer("aaa","123456",500,"222");
		cus[2]=new Customer("read","123456",0,"333");
		cus[3]=new Customer("laownag","123456",0,"444");
		cus[4]=new Customer("zhangfei","123456",54045,"555");
		admins[0]=new Admin("admin","admin");
		admins[1]=new Admin("user","123456");
		admins[2]=new Admin("aa","11");

	}
	//判断管理员登入
	public boolean selectAdminByLogin(Admin admin) {
		for(int i=0;i<admins.length;i++) {
			if(admins[i]!=null&&admins[i].getAdmin_name().equals(admin.getAdmin_name())&&admins[i].getAdmin_password().equals(admin.getAdmin_password())) {
				return true;
			}
		}
		return false;
	}
	//显示所有的用户
	public Customer[]SelsAllCus(){
		return cus;
	}
	//检查重名
	public boolean checkName(String name) {
		for(int i=1;i<cus.length;i++) {
			if(cus[i]!=null&&cus[i].getUserName().equals(name)) {
				return true;
			}
		}
		return false;
	}
	//新增用户
	public int insertCus(Customer c) {
		boolean f=checkName(c.getUserName());
		if(!f) {
			for(int i=0;i<cus.length;i++) {
				if(cus[i]==null) {
					cus[i]=c;
					return 1;
				}
			}
		}else {
			return -1;
		}
		return 0;
	}

	//删除用户
	public String delCusByName(String name) {
		for (int i = 0; i < cus.length; i++) {
			if (cus[i] != null && cus[i].getUserName().equals(name)) {
				if (cus[i].getMoney() == 0) {
					cus[i] = null;
					return "-----删除成功----";
				} else {
					return "----删除失败，用户有余额----";
				}

			}
		}
		return "----用户不存在----";
	}
	//修改密码
	public int updateCustomer(Customer c) {
		int p=0;int t=0;
		boolean f=false;
		for(int i=0;i<cus.length;i++) {
			if(cus[i]!=null&&cus[i].getUserName().equals(c.getUserName())) {
				if(c.getPassWord()!=null) {
					cus[i].setPassWord(c.getPassWord());
					p=1;
				}
				if(c.getTel()!=null) {
					cus[i].setTel(c.getTel());
					t=1;
				}
				f=true;
				break;
			}

		}
		if(p==1&&t==1) {
			return 1;
		}else if(!f) {
			return -1;
		}
		return 0;
	}
	//判断用户登入
	public boolean selectCustomerByLogin(Customer c) {
		for(int i=0;i<cus.length;i++) {
			if(cus[i]!=null&&cus[i].getUserName().equals(c.getUserName())&&cus[i].getPassWord().equals(c.getPassWord())) {
				return true;
			}
		}
		return false;
	}
	//用户取钱
	public String outMoney(String name, double money) {
		if(money>=0) {
			for (int i = 0; i < cus.length; i++) {
				if (cus[i] != null && cus[i].getUserName().equals(name)) {
					if (cus[i].getMoney() >= money) {
						cus[i].setMoney(cus[i].getMoney() - money);
						return "取钱成功";
					} else {
						return "余额不足";
					}
				}
			}
		}

		return "取钱失败";
	}
	//用户存钱
	public String addMoney(String name, double money) {
		if (money >= 0) {
			for (int i = 0; i < cus.length; i++) {
				if (cus[i] != null && cus[i].getUserName().equals(name)) {
					cus[i].setMoney(cus[i].getMoney() + money);
					return "存钱成功";
				}
			}
		}
		return "存钱失败";
	}
	//查看余额
	public String showCusMoney(String name) {
		for(int i=0;i<cus.length;i++) {
			if (cus[i] != null && cus[i].getUserName().equals(name)) {
				return "余额为："+cus[i].getMoney();

			}
		}
		return null;
	}
	//修改密码
	public String modCusPass(String name,String password) {
		for(int i=0;i<cus.length;i++) {
			if(cus[i]!=null&cus[i].getUserName().equals(name)) {
				cus[i].setPassWord(password);
				return "修改成功";
			}
		}
		return "修改失败";

	}
	//显示用户信息
	public String showAllCustomer(String name) {
		for(int i=0;i<cus.length;i++) {
			if(cus[i]!=null&&cus[i].getUserName().equals(name)) {

				return "UserName:"+cus[i].getUserName()+"\t"+"Money:"+cus[i].getMoney()+"\t"+"Tel:"+cus[i].getTel();
			}
		}
		return "----显示失败-----";
	}
	//网上转账
	public int transferOut(String name,double money) {
		for (int i = 0; i < cus.length; i++) {
			if (cus[i] != null && cus[i].getUserName().equals(name)) {
				if (cus[i].getMoney() >= money) {
					cus[i].setMoney(cus[i].getMoney() - money);
					return 1;
				} else {
					return 0;
				}
			}
		}
		return -1;
	}
	public int transferInt(String name,double money,boolean f) {
		if(f) {
			if (money >= 0) {
				for (int i = 0; i < cus.length; i++) {
					if (cus[i] != null && cus[i].getUserName().equals(name)) {
						cus[i].setMoney(cus[i].getMoney() + money);
						return 1;
					}
				}
			}
			return -1;
		}
		return 0;
	}
	//检查是否存在该用户
	public boolean idName(String name) {
		for (int i = 0; i < cus.length; i++) {
			if(cus[i] != null && cus[i].getUserName().equals(name)) {
				return true;
			}
		}
		return false;
	}
	public Customer[] getCus() {
		return cus;
	}

	public Admin[] getAdmins() {
		return admins;
	}







}

